package weaver.interfaces.expense.action.zcpz;

import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.expense.ExpenseImp;
import weaver.interfaces.expense.OAExpenseBean;
import weaver.interfaces.util.ExpenseCompare;
import weaver.interfaces.util.ExpenseCompareUtil;
import weaver.interfaces.util.ExpenseInfoUtil;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

/**
 * 
 * @ClassName: ZCPZ_SJHC_Voucher
 * @Description:支出凭证-试剂耗损
 * @author xiyufei
 * @date 2017-3-20 上午11:19:21
 *
 */
public class ZCPZ_SJHC_Voucher extends BaseBean implements Action {

	public String execute(RequestInfo requestInfo) {
		try {
			String workflowid = requestInfo.getWorkflowid();
			String requestid = requestInfo.getRequestid();
			writeLog("进入ZCPZ_SJHC_Voucher-----------" + requestid);
			RecordSet rs = new RecordSet();
			rs.executeSql("select formid from workflow_base where id = " + workflowid);
			rs.next();
			String formid = rs.getString("formid");
			String formtable = "formtable_main_" + formid.replaceAll("-", "");

			String ccode = "";
			double amount = 0;
			String cexpenseitemcode = "";
			String citemcode = "";
			String ly = "";
			String iyear = "";
			// System.out.println("select
			// id,ks,cexpenseitemcode,citemcode,zje,zjly,sqrq,kth from
			// "+formtable+" where requestid="+requestid);
			rs.execute("select id,ks,zcxm,pjje,ly,sqrq,kth1,liucheng,zf,huansuan,shouk,bh from " + formtable
					+ " where requestid=" + requestid);
			rs.next();
			ExpenseInfoUtil expenseInfoUtil = new ExpenseInfoUtil();
			ccode = expenseInfoUtil.getKsbm(Util.null2String(rs.getString("ks")));
			String cname = expenseInfoUtil.getCname(ccode);
			amount = Util.getDoubleValue(rs.getString("pjje").replace(",", ""), 0);

			amount = Util.getDoubleValue(rs.getString("huansuan").replace(",", ""), 0);

			String citemname = expenseInfoUtil.getCtn(citemcode);
			cexpenseitemcode = Util.null2String(rs.getString("zcxm"));
			String zf = Util.null2String(rs.getString("zf"));

			String cexpenseitemname = expenseInfoUtil.getCItemName(cexpenseitemcode);
			String kth = Util.null2String(rs.getString("kth1"));
			ly = Util.null2String(rs.getString("ly"));
			iyear = Util.null2String(rs.getString("sqrq"));

			// 收款单位
			String shouk = Util.null2String(rs.getString("shouk"));
			String sql = "select distinct danw from formtable_main_207 where cVenCode = '" + shouk + "'";
			rs.execute(sql);
			rs.next();
			// 收款单位中文名
			String shoukstr = Util.null2String(rs.getString("danw"));
			// 流程编号
			String bh = Util.null2String(rs.getString("bh"));

			OAExpenseBean oaExpenseBean = new OAExpenseBean();
			oaExpenseBean.setAmount(amount + "");
			oaExpenseBean.setBilldate(iyear);
			oaExpenseBean.setBilltype("0");
			oaExpenseBean.setCcode(ccode);
			oaExpenseBean.setCname(cname);
			oaExpenseBean.setCexpenseitemcode(cexpenseitemcode);
			oaExpenseBean.setCexpenseitemname(cexpenseitemname);
			oaExpenseBean.setCitemcode(citemcode);

			// 判断项目编号是否为空，如果为空就传递课题号
			if (null == citemcode || "".equals(citemcode)) {
				oaExpenseBean.setCitemcode(kth);
			}

			// 流程编号
			oaExpenseBean.setBh(bh);

			oaExpenseBean.setCitemname(citemname);
			oaExpenseBean.setLy(ly);
			oaExpenseBean.setZffs(zf);
			oaExpenseBean.setRequestid(requestid);
			oaExpenseBean.setSuppliercode(shouk);
			oaExpenseBean.setSuppliername(shoukstr);
			ExpenseImp expenseImp = new ExpenseImp();
			expenseImp.insetExpense(oaExpenseBean);

		} catch (Exception e) {
			requestInfo.getRequestManager().setMessageid("111");
			requestInfo.getRequestManager().setMessagecontent(e.getMessage());
			return FAILURE_AND_CONTINUE;
		}

		return SUCCESS;
	}

}
